﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace BuddHa.Wechat.Mini.Protocols
{
    public static class WechatProtocol
    {
        public static string BuildQueryString(this IDictionary<string, string> keyValuePairs, bool urlEncoding = false)
        {
            var stringBuilder = new StringBuilder();
            foreach (var item in keyValuePairs)
            {
                if (urlEncoding)
                {
                    stringBuilder.Append($"{item.Key}={HttpUtility.UrlEncode(item.Value, Encoding.UTF8)}&");
                }
                else
                {
                    stringBuilder.Append($"{item.Key}={item.Value}&");
                }
            }
            return stringBuilder.ToString().TrimEnd('&');
        }

        public static string NotifyReturn(bool isSuccess)
        {
            var gatewayData = new GatewayData();
            if (isSuccess == false)
            {
                gatewayData.Add("return_code", "FAILD");
                gatewayData.Add("return_msg", "Notify Error");
                return gatewayData.ToXml();
            }
            gatewayData.Add("return_code", "SUCCESS");
            gatewayData.Add("return_msg", "OK");
            return gatewayData.ToXml();
        }
    }
}
